#include "WindowControl.h"
#include <QIcon>
#include <QPainter>
#include <QPen>
#include "config.h"

#include <QResource>

WindowControl::WindowControl(QWidget *parent)
{
    QResource::registerResource(GAME_RES_PATH);

    // 每1s检查一下有没有通过某一关
    timer.setInterval(1000);
    start = new StartScene;
    play = new PlayScene;
    round = new RoundScene;
    choose = new ChooseScene;

    win = new WinScene;

    start->show();
    //  放音乐
    play->music();
    // 设置界面间的联系
    connections();
}

WindowControl::~WindowControl()
{
    delete play;
    delete round;
    delete choose;
}

void WindowControl::CheckPassCertainRound()
{
    // 开启计时器
    timer.start();
    // 每1s检查一下有没有通过某一关
    connect(&timer, &QTimer::timeout, this, [=]()
    {
        if(play->hub->getCurrent_have() >= play->hub->getNeed())
        {
            //如果通过了某关
            //更新关卡数
            round->level++;
            play->round++;
            if(round->level >= 5)
            {
                round->level = 5;
                play->round = 5;
                play->close();
                choose->close();
                round->close();
                win->show();
                QSettings setting("Parameter.ini", QSettings::IniFormat);
                setting.clear();
            }
            else
            {
                //请用户选择局部强化的建筑
                play->close();
                choose->show();
                //hub更新目标
                play->hub->UpdateNeed();
            }
        }
    });
}

void WindowControl::ChooseUpgradeMiner()
{
    // 更新miner，belt，cutter速率
    play->upgrade = BuildingType::MINER;
    for (int i = 0; i < play->buildings.size(); i++)
    {
        switch (play->buildings[i]->name)
        {
            case BuildingType::MINER:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::MINER_SPEED_2;
                break;
            case BuildingType::CUTTER:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::CUTTER_SPEED_1;
                break;
            case BuildingType::BELT_A:
            case BuildingType::BELT_A_S:
            case BuildingType::BELT_A_W:
            case BuildingType::BELT_D:
            case BuildingType::BELT_D_S:
            case BuildingType::BELT_D_W:
            case BuildingType::BELT_S:
            case BuildingType::BELT_S_A:
            case BuildingType::BELT_S_D:
            case BuildingType::BELT_W:
            case BuildingType::BELT_W_A:
            case BuildingType::BELT_W_D:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::BELT_SPEED_1;
                break;
            default:
                break;
        }
    }
}

void WindowControl::ChooseUpgradeBelt()
{
    // 更新miner，belt，cutter速率
    play->upgrade = BuildingType::BELT;
    for (int i = 0; i < play->buildings.size(); i++)
    {
        switch (play->buildings[i]->name)
        {
            case BuildingType::MINER:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::MINER_SPEED_1;
                break;
            case BuildingType::CUTTER:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::CUTTER_SPEED_1;
                break;
            case BuildingType::BELT_A:
            case BuildingType::BELT_A_S:
            case BuildingType::BELT_A_W:
            case BuildingType::BELT_D:
            case BuildingType::BELT_D_S:
            case BuildingType::BELT_D_W:
            case BuildingType::BELT_S:
            case BuildingType::BELT_S_A:
            case BuildingType::BELT_S_D:
            case BuildingType::BELT_W:
            case BuildingType::BELT_W_A:
            case BuildingType::BELT_W_D:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::BELT_SPEED_2;
                break;
            default:
                break;
        }
    }
}
void WindowControl::ChooseUpgradeCutter()
{
    // 更新miner，belt，cutter速率
    play->upgrade = BuildingType::CUTTER;
    for (int i = 0; i < play->buildings.size(); i++)
    {
        switch (play->buildings[i]->name)
        {
            case BuildingType::MINER:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::MINER_SPEED_1;
                break;
            case BuildingType::CUTTER:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::CUTTER_SPEED_2;
                break;
            case BuildingType::BELT_A:
            case BuildingType::BELT_A_S:
            case BuildingType::BELT_A_W:
            case BuildingType::BELT_D:
            case BuildingType::BELT_D_S:
            case BuildingType::BELT_D_W:
            case BuildingType::BELT_S:
            case BuildingType::BELT_S_A:
            case BuildingType::BELT_S_D:
            case BuildingType::BELT_W:
            case BuildingType::BELT_W_A:
            case BuildingType::BELT_W_D:
                play->buildings[i]->FirstRequire_ms = BuildingSpeed::BELT_SPEED_1;
                break;
            default:
                break;
        }
    }
}
void WindowControl::connections()
{
    // start界面选择新游戏
    connect(start, &StartScene::newName, this, [=]()
    {
        start->close();
        QSettings setting("Parameter.ini", QSettings::IniFormat);
        setting.clear();
        play->saved = false;
        play->LoadSave();
        round->level = play->round;
        // 开启计时器，每1s检查一下有没有通过某一关
        CheckPassCertainRound();
        round->show();
    });

    // start界面选择继续游戏
    connect(start, &StartScene::continueGame, this, [=]()
    {
        start->close();
        play->LoadSave();
        round->level = play->round;
        // 开启计时器，每1s检查一下有没有通过某一关
        CheckPassCertainRound();
        if (round->level < 5)
        {
            round->show();
        }
    });

    // round界面确认键按下后，进入游戏
    connect(round->confirm, &QPushButton::clicked, this, [=]()
    {
        round->close();
        play->show();
    });


    // choose界面选择局部强化miner
    connect(&choose->choose_miner, &QPushButton::clicked, this, [=]()
    {
        choose->close();
        round->show();
        ChooseUpgradeMiner();
    });

    // choose界面选择局部强化belt
    connect(&choose->choose_belt, &QPushButton::clicked, this, [=]()
    {
        choose->close();
        round->show();
        ChooseUpgradeBelt();
    });

    // choose界面选择局部强化cutter
    connect(&choose->choose_cutter, &QPushButton::clicked, this, [=]()
    {
        choose->close();
        round->show();
        ChooseUpgradeCutter();
    });
}
